package com.hliushi.movie.web;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.hliushi.movie.entity.MoviePicClip;
import com.hliushi.movie.service.MovieDetailService;
import com.hliushi.movie.service.MoviePicClipService;
import com.itheima.pinda.base.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 推剪辑和图集模块管理
 *
 * @author llhuang10
 * @date 2021/10/13 20:54
 */
@Slf4j
@Api(tags = "推剪辑和图集模块管理")
@Validated
@RestController
@RequestMapping("/api/tui")
public class MovieTuiInfoController {

    @Resource
    private MoviePicClipService moviePicClipService;

    @Resource
    private MovieDetailService movieDetailService;

    /**
     * 根据id获取推信息
     *
     * @param id 推信息的Id
     * @return R
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取推信息")
    public R<MoviePicClip> getById(@PathVariable("id") String id) {
        MoviePicClip moviePicClip = moviePicClipService.getById(id);
        return moviePicClip != null ? R.success(moviePicClip) : R.fail("查询失败");
    }

    /**
     * 新添一个电影的推信息
     *
     * @param moviePicClip 推信息
     * @return R
     */
    @PostMapping("")
    @ApiOperation("新添一个电影的推信息")
    public R insertTuiInfo(@Valid MoviePicClip moviePicClip) {
        boolean res = moviePicClipService.insertTuiInfo(moviePicClip);
        return res ? R.success() : R.fail("添加电影的推信息失败, 请联系管理员!");
    }

    /**
     * 修改一个电影的推信息
     *
     * @param moviePicClip 推信息
     * @return R
     */
    @PutMapping
    @ApiOperation("修改一个电影的推信息")
    public R updateTuiInfo(@Valid MoviePicClip moviePicClip) {
        boolean res = moviePicClipService.updateTuiInfo(moviePicClip);
        return res ? R.success() : R.fail("更新电影的推信息失败, 请联系管理员!");
    }

    /**
     * 根据电影id和类型id获取电影的推信息
     *
     * @param movieId 电影id
     * @param type    类型id
     * @return 电影的推信息
     */
    @GetMapping("/get")
    @ApiOperation("根据电影id和类型id获取电影的推信息")
    public R<List<MoviePicClip>> getByMovieIdAndType(@RequestParam(value = "movieId", required = false) Integer movieId,
                                                     @RequestParam(value = "type", required = false) Integer type) {
        List<MoviePicClip> res = movieDetailService.getByMovieIdAndType(movieId, type);
        return CollectionUtils.isNotEmpty(res) ? R.success(res) : R.fail("查询失败");
    }
}
